﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.SessionState;

namespace RDIFramework.WebApp.Modules.hander
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.WebCommon;
    using RDIFramework.Utilities;
    using RDIFramework.ServiceAdapter;

    /// <summary>
    /// StaffAdminHandler 的摘要说明
    /// </summary>
    public class StaffAdminHandler : IHttpHandler, IRequiresSessionState
    {
        private string action
        {
            get
            {
                return RDIFramework.WebCommon.PublicMethod.GetString(getObj("action"));
            }
        }
        private object getObj(string key) { return RDIFramework.WebCommon.StringHelper.GetRequestObject(key); }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            switch (action)
            {
                case "AddStaff":
                    AddStaff(context);
                    break;
                case "EditStaff":
                    EditStaff(context);
                    break;
                case "DeleteStaff":
                    DeleteStaff(context);
                    break;
                case "treedata":
                    context.Response.Write("");
                    break;
                case "GetEntity":
                    context.Response.Write(RDIFramework.WebCommon.JSONhelper.ToJson(RDIFrameworkService.Instance.StaffService.GetEntity(Utils.UserInfo,PublicMethod.GetString(getObj("KeyId")))));
                    break;
                default:
                    GetDataList(context);
                    break;
            }
        }

        #region private PiStaffEntity GetPageStaffEntity(PiStaffEntity entity) 界面数据到员工实体
        /// <summary>
        /// 界面数据到员工实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private PiStaffEntity GetPageStaffEntity(PiStaffEntity entity)
        {
            entity.RealName = PublicMethod.GetString(getObj("RealName"));
            entity.UserName = RDIFramework.Utilities.StringHelper.GetPYString(entity.RealName);
            entity.Code = PublicMethod.GetString(getObj("Code"));
            entity.Gender = PublicMethod.GetString(getObj("Gender"));
            entity.Birthday = BusinessLogic.ConvertToDateToString(getObj("Birthday"));
            entity.Age = PublicMethod.GetString(getObj("Age"));
            entity.Major = PublicMethod.GetString(getObj("Major"));
            entity.School = PublicMethod.GetString(getObj("School"));
            entity.Education = PublicMethod.GetString(getObj("Education"));
            entity.Degree = PublicMethod.GetString(getObj("Degree"));
            entity.Title = PublicMethod.GetString(getObj("Title"));
            entity.TitleLevel = PublicMethod.GetString(getObj("TitleLevel"));
            entity.TitleDate = BusinessLogic.ConvertToDateToString(getObj("TitleDate"));
            entity.WorkingProperty = PublicMethod.GetString(getObj("WorkingProperty"));
            entity.WorkingDate = BusinessLogic.ConvertToDateToString(getObj("WorkingDate"));
            entity.IdentificationCode = PublicMethod.GetString(getObj("IdentificationCode"));
            entity.BankCode = PublicMethod.GetString(getObj("BankCode"));
            entity.JoinInDate = BusinessLogic.ConvertToDateToString(getObj("JoinInDate"));
            entity.Email = PublicMethod.GetString(getObj("Email"));
            entity.Mobile = PublicMethod.GetString(getObj("Mobile"));
            entity.ShortNumber = PublicMethod.GetString(getObj("ShortNumber"));
            entity.QICQ = PublicMethod.GetString(getObj("QICQ"));
            entity.OfficeZipCode = PublicMethod.GetString(getObj("OfficeZipCode"));
            entity.OfficePhone = PublicMethod.GetString(getObj("OfficePhone"));
            entity.OfficeFax = PublicMethod.GetString(getObj("OfficeFax"));
            entity.OfficeAddress = PublicMethod.GetString(getObj("OfficeAddress"));
            entity.NativePlace = PublicMethod.GetString(getObj("NativePlace"));
            entity.HomeZipCode = PublicMethod.GetString(getObj("HomeZipCode"));
            entity.HomeFax = PublicMethod.GetString(getObj("HomeFax"));
            entity.Party = PublicMethod.GetString(getObj("Party"));
            entity.Nation = PublicMethod.GetString(getObj("Nation"));
            entity.Nationality = PublicMethod.GetString(getObj("Nationality"));
            entity.HomePhone = PublicMethod.GetString(getObj("HomePhone"));
            entity.Telephone = PublicMethod.GetString(getObj("Telephone"));
            entity.HomeAddress = PublicMethod.GetString(getObj("HomeAddress"));

            entity.DimissionDate = BusinessLogic.ConvertToDateToString(getObj("DimissionDate"));
            entity.DimissionWhither = PublicMethod.GetString(getObj("DimissionWhither"));
            entity.DimissionCause = PublicMethod.GetString(getObj("DimissionCause"));
            entity.IsDimission = PublicMethod.GetString(getObj("IsDimission")) == "on" ? 1 : 0;  
          
            entity.Enabled = PublicMethod.GetString(getObj("Enabled")) == "on" ? 1 : 0;           
            entity.Description = PublicMethod.GetString(getObj("Description"));
            return entity;
        }
        #endregion

        #region private void AddStaff(HttpContext ctx) 新增员工（职员）
        /// <summary>
        /// 新增员工（职员）
        /// </summary>
        /// <param name="ctx"></param>
        private void AddStaff(HttpContext ctx)
        {
            string organizeId = PublicMethod.GetString(getObj("vOrganizeId"));
            PiStaffEntity entity = new PiStaffEntity();
            UserInfo vUser = Utils.UserInfo;
            entity = this.GetPageStaffEntity(entity);
            entity.DeleteMark = 0;
            if (vUser != null)
            {
                entity.CreateBy = vUser.RealName;
                entity.CreateUserId = vUser.Id;
            }
            string statusCode = string.Empty;
            string statusMessage = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(organizeId))
                {
                    RDIFrameworkService.Instance.StaffService.Add(vUser, entity, out statusCode, out statusMessage);
                }
                else
                {
                    RDIFrameworkService.Instance.StaffService.Add(vUser, entity, out statusCode, out statusMessage, organizeId);
                }

                if (statusCode == StatusCode.OKAdd.ToString())
                {
                    ctx.Response.Write(new JsonMessage { Success = true, Data = "1", Message = statusMessage }.ToString());
                }
                else
                {
                    ctx.Response.Write(new JsonMessage { Success = false, Data = "0", Message = statusMessage }.ToString());
                }
            }
            catch (Exception ex)
            {
                ctx.Response.Write(new JsonMessage { Success = false, Data = "0", Message = statusMessage + ex.Message }.ToString());
            }
        }
        #endregion 

        #region private void EditStaff(HttpContext ctx) 修改员工（职员）
        /// <summary>
        /// 修改员工（职员）
        /// </summary>
        /// <param name="ctx"></param>
        private void EditStaff(HttpContext ctx)
        {
            UserInfo vUser = Utils.UserInfo;
            string vId = PublicMethod.GetString(getObj("KeyId"));
            PiStaffEntity entity = RDIFrameworkService.Instance.StaffService.GetEntity(vUser, vId);
            entity = this.GetPageStaffEntity(entity);
            if (vUser != null)
            {
                entity.ModifiedBy = vUser.RealName;
                entity.ModifyUserId = vUser.Id;
            }

            string statusCode = string.Empty;
            string statusMessage = string.Empty;
            try
            {
                RDIFrameworkService.Instance.StaffService.UpdateStaff(vUser, entity, out statusCode, out statusMessage);

                if (statusCode == StatusCode.OKUpdate.ToString())
                {
                    ctx.Response.Write(new JsonMessage { Success = true, Data = "1", Message = statusMessage }.ToString());
                }
                else
                {
                    ctx.Response.Write(new JsonMessage { Success = false, Data = "0", Message = statusMessage }.ToString());
                }
            }
            catch (Exception ex)
            {
                ctx.Response.Write(new JsonMessage { Success = false, Data = "0", Message = statusMessage + ex.Message }.ToString());
            }
        }
        #endregion

        #region private void DeleteStaff(HttpContext ctx) 删除员工（职员）
        /// <summary>
        /// 删除员工（职员）
        /// </summary>
        /// <param name="ctx"></param>
        private void DeleteStaff(HttpContext ctx)
        {
            string vId = PublicMethod.GetString(getObj("KeyId"));
            UserInfo vUser = Utils.UserInfo;

            int successFlag = 0;
            try
            {
                int returnValue = RDIFrameworkService.Instance.StaffService.SetDeleted(Utils.UserInfo, new string[] { vId });

                if (returnValue > 0)
                {
                    successFlag = 1;
                    ctx.Response.Write(new JsonMessage { Success = true, Data = successFlag.ToString(), Message = RDIFrameworkMessage.MSG0013 }.ToString());
                }
                else
                {
                    ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = RDIFrameworkMessage.MSG3020 }.ToString());
                }
            }
            catch (Exception ex)
            {
                ctx.Response.Write(new JsonMessage { Success = false, Data = successFlag.ToString(), Message = RDIFrameworkMessage.MSG3020 + ex.Message }.ToString());
            } 
        }
        #endregion

        private void GetDataList(HttpContext context)
        {      
            string organizeId = RDIFramework.WebCommon.PublicMethod.GetString(getObj("organizeId"));
            string json = "[]";
            if(!string.IsNullOrEmpty(organizeId))
            {
                DataTable dtStaff = RDIFramework.ServiceAdapter.RDIFrameworkService.Instance.StaffService.GetDTByOrganize(Utils.UserInfo, organizeId, true);
                json = RDIFramework.WebCommon.JSONhelper.ToJson(dtStaff);
            }
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        } 
    }
}